package com.situ.company.project.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.project.dao.IProjectDao;
import com.situ.company.project.model.ProjectModel;
import com.situ.company.util.JDBCUtil;

import util.FmtEmpty;

public class ProjectDaoImpl implements IProjectDao {
	private static String table="project";
	private static String cols="code,name,time,time2";
	@Override
	public Integer insert(ProjectModel model) {
		StringBuffer sql=new StringBuffer("insert into ");
		sql.append(table).append('(').append(cols).append(")values(?,?,?,?)");
		System.out.println(model.getCode());
		System.out.println(model.getName());
		System.out.println(model.getStTime());
		return JDBCUtil.update(sql.toString(),model.getCode(),model.getName(),model.getStTime(),model.getEndTime());
	}

	@Override
	public Integer deleteByCode(ProjectModel model) {
		StringBuffer sql=new StringBuffer("delete from ");
		sql.append(table).append(" where code= ?");
		return JDBCUtil.update(sql.toString(),model.getCode());
	}

	@Override
	public Integer delete(ProjectModel model) {
		StringBuffer sql=new StringBuffer("delete from ").append(table);
		System.out.println(model.toString());
		List<Object> values=appendWhere(sql, model);
		
		return JDBCUtil.update(sql.toString(),values);
	}
	
	private List<Object> appendWhere(StringBuffer sql, ProjectModel model) {
		sql.append(" where 1=1 ");
		List<Object> values =new ArrayList<>();
		String code =model.getCode();
		if(!FmtEmpty.isEmpty(code)) {
			sql.append("and code like ?");
			values.add(code);
		}
		String name=model.getName();
		if(!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ?");
			values.add(name);
		}
		String time=model.getStTime();
		if(time!=null) sql.append(" and time like ?");
		String time2=model.getEndTime();
		if(time2!=null) sql.append(" and time2 like ?");
		System.out.println("values="+values);
		return values;
	}

	@Override
	public Integer updateAll(ProjectModel model) {
		StringBuffer sql=new StringBuffer("update ");
		sql.append(table).append(" set name=?,time=?,time2=?,where code=?");
		return JDBCUtil.update(sql.toString(),model.getName(),model.getStTime(),model.getEndTime(),model.getCode());

	}

	@Override
	public Integer updateActive(ProjectModel model) {
		StringBuffer sql=new StringBuffer("update ");
		sql.append(table).append(" set id=id");
		List<Object> values=appendSet(sql,model);
		return JDBCUtil.update(sql.toString(),values);
	}

	private List<Object> appendSet(StringBuffer sql, ProjectModel model) {
		List<Object> values=new ArrayList<>();
		String name=model.getName();
		if(name !=null) {
			sql.append(",name=?");
			values.add(name);
		}
		String StTime=model.getStTime();
		if(StTime !=null) {
			sql.append(",time=?");
			values.add(StTime);
		}
		String EndTime=model.getEndTime();
		if(EndTime !=null) {
			sql.append(",time2=?");
			values.add(EndTime);
		}
		sql.append(" where code=?");
		values.add(model.getCode());
		return values;
	}

	@Override
	public List<ProjectModel> selectList(ProjectModel model) {
		StringBuffer sql=new StringBuffer("select id,").append(cols).append(" from ").append(table);
		List<Object> values=appendWhere(sql,model);
		 Map<String, String> map = new HashMap<>();
	        map.put("id", "id");
	        map.put("code", "code");
	        map.put("name", "name");
	        map.put("StTime", "time");
	        map.put("EndTime","time2");
		return JDBCUtil.queryList(sql.toString(), values, ProjectModel.class,map);
	}

	@Override
	public ProjectModel selectModel(ProjectModel model) {
		StringBuffer sql=new StringBuffer("select id,");
		sql.append(cols).append(" from ").append(table).append(" where code=?");
		List<Object> values=Arrays.asList(model.getCode());
		 Map<String, String> map = new HashMap<>();
	        map.put("id", "id");
	        map.put("code", "code");
	        map.put("name", "name");
	        map.put("StTime", "time");
	        map.put("EndTime","time2");
		return JDBCUtil.queryModel(sql.toString(), values,ProjectModel.class,map);
	}

	@Override
	public Integer selectCount(ProjectModel model) {
		StringBuffer sql=new StringBuffer("select count(1) from project ");
		List<Object> vals = appendWhere(sql,model);
		return JDBCUtil.queryInt(sql.toString(), vals);
	}

}
